<?php
session_start();
require_once('path.php');
require_once(SOJ_ROOT.'/include/function/lib.php');

require_once('Log.php') ;

@$request = $_POST;

//参数有效性验证

//必填项目为空就定义为错误
foreach( $request as $k => $v ){

	if ( empty( $v ) ){
	
		if( $k == 'username' || $k == 'passwd' || $k == 're_passwd' || $k == 'email' ){
		
			$_SESSION['errors'][$k] = true ;	
		}
	}
}
if ( !empty($_SESSION['errors']) ){

	$_SESSION['errors']['message'] = 'Please enter the correct information in the highlighted fields:';
	header('Location:signup.php');
	exit ;
}
//判断两次密码是否相同
if ( $request['passwd'] != $request['re_passwd'] ){

	$_SESSION['errors']['passwd'] = true ;
	$_SESSION['errors']['re_passwd'] = true ;

	$_SESSION['errors']['message']['passwd'] = 'Please verify your password again.';
}
//email地址合法性的判断
if ( !check_email_address( $request['email'] ) ){
	
	$_SESSION['errors']['email'] = true ;
	$_SESSION['errors']['message']['email'] = 'Please verify your E-MAIL address again.';
}
//用户名是否已经存在的判断
if( user_signup_process::exist_user( db_connect() , $request['username'] ) ){
	
	$_SESSION['errors']['username'] = true;
	$_SESSION['errors']['message']['username'] = "This username has existed.";

}

//@todo 此处有重复的存在
if ( !empty($_SESSION['errors'] ) ){

	header('Location:signup.php');
	exit ;
}


try{
	$user_sign_process = new user_signup_process( db_connect() , $request );
	
	if ( $user_sign_process->do_signup() ){
		
		//注册成功
		unset( $_SESSION['errors'] ) ;
		//传递给sign_success页面的信息,
		//表明是注册成功
		$_SESSION['success_message'] = "Sign up success";
		header('Location:../message_handle/success_handle.php');
		exit ;
	
	}else{
		
		$_SESSION['fail_message'] = 'signup failed' ;
		header('Location:../message_handle/fail_handle.php');
		exit ;
	}

}catch(Exception $e){
	
	exception_handle( $e ) ;
}
